/*
* Copyright 2011 Joshua Carbonneau
*
* This file is part of Texture Chaos.
*
* Texture Chaos is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Texture Chaos is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Texture Chaos.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <png.h>

#ifndef SIMPLEPNG_H
#define SIMPLEPNG_H

/************************************************
* Holds the info for a single png file, and     *
* can be used to write that info to a png image *
*************************************************/
struct SimplePng
{
    // Filename of the png file it represents
    char* filename;

    // 0 if it has been used to read in a png, 1 if it was used to write one.
    // Used for deallocation.
    int type;

    // Structures used to hold the png data
    png_structp pngstr;
    png_infop pnginf;
    png_infop pngend;

    // Holds the actual image data
    png_bytep* rowPointers;

    // Info about the image
    png_uint_32 width;
    png_uint_32 height;
    int bitdepth;
    int colortype;
    int filtermethod;
    int compressiontype;
    int interlacetype;
    int pixelSize;  // Number of bytes used to hold each pixel
};

/**************************************************************
* Create a new SimplePng from the given png file              *
* filename: The png image to read in                          *
* returns: A pointer to the new SimplePng, or null on failure *
***************************************************************/
struct SimplePng* newSimplePng(char* filename);

/*************************************************
* Deallocate a SimplePng                         *
* sp: Pointer to the SimplePng to be deallocated *
**************************************************/
void destroySimplePng(struct SimplePng* sp);

/***********************************************     
* Write the data contained in sp to a png file *
* sp: The SimplePng to write out               *
* filename: The file to write to               *
* returns: 1 on sucess, 0 on failure           *
************************************************/
int writePngFile(struct SimplePng* sp, char* filename);

/********************************************************************
* Colorize the specified rectangle in the image by setting          *
* all color channels but one to zero                                *
* sp: The SimplePng to colorize                                     *
* tly: The y coordinate of the top left corner of the rectangle     *
* tlx: The x coordinate of the top left corner of the rectangle     *
* bry: The y coordinate of the bottom right corner of the rectangle *
* brx: The x coordinate of the bottom right corner of the rectangle *
* color: The color channel to leave on                              *
*********************************************************************/
void colorizeBlock(struct SimplePng* sp, int tly, int tlx, int bry, int brx, int color);

/**************************************************************************
* Switch the contents of two specified rectangles in an image             *
* sp: The SimplePng to be effected                                        *
* tly: The y coordinate of the top left corner of the first rectangle     *
* tlx: The x coordinate of the top left corner of the first rectangle     *
* bry: The y coordinate of the bottom right corner of the first rectangle *
* brx: The x coordinate of the bottom right corner of the first rectangle *
* tlyb: The y coordinate of the top left corner of the second rectangle   *
* tlxb: The x coordinate of the top left corner of the second rectangle   *
***************************************************************************/
void switchBlocks(struct SimplePng* sp, int tly, int tlx, int bry, int brx, int tlyb, int tlxb);

/********************************************************************
* Shift the RGB values of a specified rectangle one position to     *
* the right                                                         *
* sp: The SimplePng to be effected                                  *
* tly: The y coordinate of the top left corner of the rectangle     *
* tlx: The x coordinate of the top left corner of the rectangle     *
* bry: The y coordinate of the bottom right corner of the rectangle *
* brx: The x coordinate of the bottom right corner of the rectangle *
*********************************************************************/
void shiftColors(struct SimplePng* sp, int tly, int tlx, int bry, int brx);

/********************************************************************
* Randomly set one of the R, G, or B values in a specified          *
* rectangle to a random number                                      *
* sp: The SimplePng to be effected                                  *
* tly: The y coordinate of the top left corner of the rectangle     *
* tlx: The x coordinate of the top left corner of the rectangle     *
* bry: The y coordinate of the bottom right corner of the rectangle *
* brx: The x coordinate of the bottom right corner of the rectangle *
*********************************************************************/
void randomizeColor(struct SimplePng* sp, int tly, int tlx, int bry, int brx);

/********************************************************************
* Average the color of all pixels in a specified rectangle and set  *
* all pixels to that color                                          *
* sp: The SimplePng to be effected                                  *
* tly: The y coordinate of the top left corner of the rectangle     *
* tlx: The x coordinate of the top left corner of the rectangle     *
* bry: The y coordinate of the bottom right corner of the rectangle *
* brx: The x coordinate of the bottom right corner of the rectangle *
*********************************************************************/
void averageColor(struct SimplePng* sp, int tly, int tlx, int bry, int brx);
#endif
